local skel = fk.CreateSkill {
  name = "emo__yange",
}

Fk:loadTranslationTable{
  ["emo__yange"] = "燕歌",
  [":emo__yange"] = "每回合限一次，当你不因使用而失去手牌时，你可以视为使用一张目标不大于X的普通锦囊牌（X为失去的牌数）。",

  ["#emo__yange-use"] = "燕歌：视为使用一张目标不大于 %arg 的普通锦囊牌",
  ["emo__yange_active"] = "燕歌",
}

skel:addEffect(fk.AfterCardsMove, {
  anim_type = "offensive",
  times = function (_, player)
    return 1 - player:usedSkillTimes(skel.name, Player.HistoryTurn)
  end,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) and player:usedSkillTimes(skel.name, Player.HistoryTurn) == 0 then
      for _, move in ipairs(data) do
        if move.from == player and move.moveReason ~= fk.ReasonUse then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand then
              return true
            end
          end
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local n = 0
    for _, move in ipairs(data) do
      if move.from == player and move.moveReason ~= fk.ReasonUse then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand then
            n = n + 1
          end
        end
      end
    end
    if n == 0 then return false end
    local success, dat = player.room:askToUseActiveSkill(player, {
      skill_name = "emo__yange_active", prompt = "#emo__yange-use:::"..n, extra_data = { emo__yange_num = n  }
    })
    if success and dat then
      event:setCostData(self, { tos = dat.targets, name = dat.interaction })
      return true
    end
  end,
  on_use = function(self, event, target, player, _)
    local room = player.room
    local dat = event:getCostData(self)
    local card = Fk:cloneCard(dat.name)
    card.skillName = skel.name
    local use = {
      from = player,
      tos = dat.tos,
      card = card,
    }
    room:useCard(use)
  end,
})

local skel2 = fk.CreateSkill {
  name = "emo__yange_active",
}

skel2:addEffect("active", {
  interaction = function(self, player)
    local all_choices = Fk:getAllCardNames("t")
    local x = self.emo__yange_num
    if x == nil then return end
    local choices = table.filter(all_choices, function(name)
      local card = Fk:cloneCard(name)
      card.skillName = skel.name
      if card:isCommonTrick() then
        if player:canUse(card) and not player:prohibitUse(card) then
          -- 限制固定目标的牌的目标数
          if card:getFixedTargets(player) and #card:getAvailableTargets(player) > x then return false end
          return true
        end
      end
    end)
    if #choices == 0 then return end
    return UI.CardNameBox { choices = choices }
  end,
  card_filter = Util.FalseFunc,
  target_filter = function (self, player, to_select, selected, selected_cards)
    local x = self.emo__yange_num
    if self.interaction.data == nil or x == nil then return false end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = skel.name
    -- 可选目标的牌需要受到目标数限制，借刀除外
    if card:getFixedTargets(player) == nil and card.skill:getMinTargetNum(player) < 2 then
      if #selected >= x then return false end
    end
    return card.skill:targetFilter(player, to_select, selected, selected_cards, card)
  end,
  feasible = function (self, player, selected, selected_cards)
    if self.interaction.data == nil then return false end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = skel.name
    return card.skill:feasible(player, selected, selected_cards, card)
  end,
})

return {skel, skel2}
